<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>关键词管理</span>
      </div>
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <div class="app-container">
          <div class="filter-container">
            <el-select
              v-model="where.bazaarId"
              class="filter-item"
              clearable
              placeholder="推广行业"
              style="width: 200px"
              @change="reload"
            >
              <el-option
                v-for="item in BazaarList"
                :key="item.id"
                :disabled="item.status === 0"
                :label="item.name"
                :value="item.id"
              />
            </el-select>

            <el-select
              v-model="where.userStat"
              class="filter-item"
              clearable
              placeholder="关键词状态"
              style="width: 200px"
              @change="reload"
              @clear="reload"
            >
              <el-option
                v-for="item in userStatList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <el-input
              v-model="where.keywords"
              class="filter-item"
              clearable
              placeholder="关键词"
              style="width: 220px"
              @clear="reload"
            />
            <el-button
              class="filter-item"
              icon="el-icon-search"
              type="primary"
              @click="reload"
              >搜索
            </el-button>

            <el-button
              class="filter-item"
              icon="el-icon-refresh"
              style="margin-left: 2px"
              @click="reset"
              >重置
            </el-button>
          </div>
        </div>
      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :parse-data="parseData"
        :selection.sync="selection"
        :where="where"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            v-if="$hasPermission('xhskeywords:create')"
            icon="el-icon-plus"
            plain
            size="small"
            type="primary"
            @click="openEdit({})"
            >导入关键词
          </el-button>
          <el-button
            v-if="$hasPermission('xhskeywords:delete')"
            icon="el-icon-delete"
            plain
            size="small"
            type="danger"
            @click="openDelEdit({})"
            >删除导入关键词
          </el-button>
          <el-button
            v-if="$hasPermission('xhskeywords:delete')"
            :disabled="selection.length === 0"
            icon="el-icon-delete"
            size="small"
            type="danger"
            @click="removeBatch"
            >删除选中关键词
          </el-button>
          <el-button
            v-if="$hasPermission('xhskeywords:create')"
            :disabled="selection.length === 0"
            icon="el-icon-edit-outline"
            size="small"
            type="primary"
            @click="resetBatch"
            >重置选中关键词
          </el-button>
          <el-button
            v-if="$hasPermission('xhskeywords:create')"
            :disabled="selection.length === 0"
            icon="el-icon-help"
            size="small"
            type="success"
            @click="openMoveEdit({})"
            >移动关键词行业
          </el-button>
        </template>

        <!-- 推广行业列 -->
        <template slot="bazaarId" slot-scope="{ row }">
          <template v-for="item in BazaarList">
            <span v-if="row.bazaarId === item.id" :key="item.id">
              {{ item.name }}
            </span>
          </template>
        </template>

        <!-- 推广行业列 -->
        <template slot="deviceId" slot-scope="{ row }">
          <template v-for="item in DeviceList">
            <span v-if="row.deviceId === item.deviceId" :key="item.deviceId">
              {{ item.name ? item.name : item.deviceId }}
            </span>
          </template>
        </template>

        <!-- 推广状态列 -->
        <template slot="status" slot-scope="{ row }">
          <ele-dot v-if="row.status == 1" :ripple="ripple" text="设备搜索中" />
          <ele-dot
            v-if="row.status == 2"
            type="success"
            :ripple="ripple"
            text="已搜索关键词"
          />
          <ele-dot
            v-if="row.status == 0"
            type="info"
            :ripple="ripple"
            text="等待搜索"
          />
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 编辑弹窗 -->
    <vue-edit :data="current" :visible.sync="showEdit" @done="reload" />

    <!-- 删除弹窗 -->
    <del-edit :data="current" :visible.sync="showDelEdit" @done="reload" />

    <!-- 移动弹窗 -->
    <vue-move :data="current" :visible.sync="showMoveEdit" @done="reload" />
  </div>
</template>

<script>
import VueEdit from "./vue-edit";
import DelEdit from "./del-edit";
import VueMove from "./vue-move";
export default {
  name: "xhsKeywords",
  components: { VueEdit, DelEdit, VueMove },
  data() {
    return {
      //市场列表
      BazaarList: [],
      // 设备列表
      DeviceList: [],
      // 闪光点
      ripple: true,
      // 表格高度
      defaultHeight: 0,
      // 推广账户状态
      userStatList: [
        { value: 0, label: "等待搜索" },
        { value: 1, label: "设备搜索中" },
        { value: 2, label: "已搜索关键词" },
      ],
      // 表格数据接口
      url: "/Xhskeywords/read",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 80,
          align: "center",
        },
        {
          prop: "bazaarId",
          label: "所属行业",
          slot: "bazaarId",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "keywords",
          label: "关键词",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "priority",
          label: "优先级",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "status",
          slot: "status",
          label: "搜索状态",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "createtime",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "datetime",
          label: "搜索时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "deviceId",
          slot: "deviceId",
          label: "搜索设备",
          sortable: "custom",
          showOverflowTooltip: true,
        },
      ],
      // 表格搜索条件
      where: {},
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示删除弹窗
      showDelEdit: false,
      // 是否显示移动弹窗
      showMoveEdit: false,
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /*表格数据加载*/
    parseData(res) {
      if (res.code === 0) {
        this.BazaarList = res.bazaar;
        this.DeviceList = res.device;
      }
      return {
        code: res.code,
        data: res.data,
        count: res.count,
        msg: res.msg,
      };
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },

    /* 显示编辑 */
    openEdit(row) {
      row.bazaar = this.BazaarList;
      this.current = row;
      this.showEdit = true;
    },

    /* 显示编辑 */
    openDelEdit(row) {
      this.current = row;
      this.showDelEdit = true;
    },

    /* 显示编辑 */
    openMoveEdit(row) {
      row.bazaar = this.BazaarList;
      row.ids = this.selection.map((d) => d.id).join(",");
      this.current = row;
      this.showMoveEdit = true;
    },

    /* 批量删除 */
    resetBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要重置选中的数据吗?", "提示", {
        confirmButtonText: "重置",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const iad = this.selection.map((d) => d.id);
          const loading = this.$loading({ lock: true });
          this.$http
            .delete("/Xhskeywords/resetkeywords", {
              data: { id: iad.join(",") },
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的数据吗?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const iad = this.selection.map((d) => d.id);
          const loading = this.$loading({ lock: true });
          this.$http
            .delete("/Xhskeywords/delete", {
              data: { id: iad.join(",") },
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.ele-table-tool .is-disabled {
  cursor: not-allowed;
  filter: Alpha(opacity=0.65);
  -moz-opacity: 0.65;
  opacity: 0.65;
}
</style>
